package com.qxzn.app.page.robot;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.BindRobotBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.R;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.Constant;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddRobotActivity3 extends BaseActivity {
    private String deviceId;
    private String pwd;
    private Handler handler = new Handler();

    @Override
    protected int initLayout() {
        return R.layout.activity_add_robot3;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("添加机器人");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        deviceId = getIntent().getStringExtra(Constant.PutExtraKey.DEVICE_ID);
        pwd = getIntent().getStringExtra(Constant.PutExtraKey.SECURITY_PWD);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bingDevice();
            }
        }, 1000);
    }

    private void bingDevice() {
        JSONObject params = new JSONObject();
        params.put("device_id", deviceId);
        params.put("desk_password", pwd);
        HttpRequest.getService().bingDevice(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean<BindRobotBean>>() {
            @Override
            public void onResponse(Call<BaseBean<BindRobotBean>> call, Response<BaseBean<BindRobotBean>> response) {
                BaseBean<BindRobotBean> baseBean = response.body();
                if (baseBean != null) {
                    if (baseBean.getCode() == 200) {
                        Intent intent = new Intent(AddRobotActivity3.this, AddRobotActivity4.class);
                        startActivity(intent);
                        finish();
                        return;
                    } else if (baseBean.getCode() == 402) {
                        Intent intent = new Intent(AddRobotActivity3.this, AddRobotNoticeActivity1.class);
                        intent.putExtra(Constant.PutExtraKey.TITLE, "该小π机器人已被绑定");
                        intent.putExtra(Constant.PutExtraKey.CONTENT, "若需要使用权限，请联系该机器人管理员");
                        startActivityForResult(intent, 2);
                        return;
//                    } else if (baseBean.getCode() == 403) {
//                        Intent intent = new Intent(AddRobotActivity3.this, WaitForAdminAuthActivity.class);
//                        intent.putExtra("data", baseBean.getData());
//                        startActivityForResult(intent, 2);
//                        return;
                    } else if (baseBean.getCode() == 404) {
                        Intent intent = new Intent(AddRobotActivity3.this, AddRobotNoticeActivity1.class);
                        intent.putExtra("type", 1);
                        intent.putExtra(Constant.PutExtraKey.TITLE, "如需浏览，请长按网址复制后使用浏览器访问");
                        intent.putExtra(Constant.PutExtraKey.CONTENT, getIntent().getStringExtra(Constant.PutExtraKey.DEVICE_ID));
                        startActivityForResult(intent, 2);
                        return;
                    }
                }
                Intent intent = new Intent(AddRobotActivity3.this, AddRobotActivity5.class);
                if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
                    intent.putExtra("message", baseBean.getMsg());
                } else {
                    intent.putExtra("message", "绑定失败！");
                }
                startActivityForResult(intent, 3);
            }

            @Override
            public void onFailure(Call<BaseBean<BindRobotBean>> call, Throwable t) {
                Intent intent = new Intent(AddRobotActivity3.this, AddRobotActivity5.class);
                intent.putExtra("message", "网络错误，请稍后再试！");
                startActivityForResult(intent, 3);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            } else {
                finish();
            }
        } else if (requestCode == 3) {
            if (resultCode == RESULT_OK) {
                if (data != null && data.getBooleanExtra("retry", false)) {
                    AppUtil.showLog(deviceId + "---" + pwd);
                    if (!TextUtils.isEmpty(deviceId) && !TextUtils.isEmpty(pwd)) {
                        bingDevice();
                    }
                } else {
                    setResult(RESULT_OK);
                    finish();
                }
            } else {
                finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
